package com.ps.controller.admin;


import com.ps.dto.MedicineDTO;
import com.ps.dto.MedicinePageQueryDTO;
import com.ps.entity.Medicine;
import com.ps.result.PageResult;
import com.ps.result.Result;
import com.ps.service.MedicineService;
import com.ps.vo.MedicineVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/medicine")
@Api(tags = "药品相关接口")
@RequiredArgsConstructor
public class MedicineControlller {

    // 注入MedicineService
    private final MedicineService medicineService;
    private final RedisTemplate redisTemplate;
    // 新增加药品
    @PostMapping
    @ApiOperation("新增加药品")
    public Result save(@RequestBody MedicineDTO medicineDTO){
        // 记录新增药品的信息
        log.info("新增药品:{}",medicineDTO);
        medicineService.saveWithForm(medicineDTO);
        String key="medicine_id"+medicineDTO.getCategoryId();
        cleanCache(key);
        // 返回成功结果
        return Result.success();
    }

    // 药品分类查询
    @GetMapping("/page")
    @ApiOperation("药品分类查询")
    public Result<PageResult> page(MedicinePageQueryDTO medicinePageQueryDTO){
        // 记录分页查询药品的信息
        log.info("分页查询药品:{}",medicinePageQueryDTO);
        // 调用medicineService的pageQuery方法，传入medicinePageQueryDTO参数，获取分页结果
        PageResult pageResult=medicineService.pageQuery(medicinePageQueryDTO);
        // 返回分页结果
        return Result.success(pageResult);
    }


    // 根据id删除药品
    @DeleteMapping
    @ApiOperation("药品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("药品批量删除：{}",ids);
        medicineService.deleteBatch(ids);
        cleanCache("medicine_*");
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询药品")
    public Result<MedicineVO> getById(@PathVariable Long id){
        log.info("根据id查询药品：{}",id);
        MedicineVO medicineVO=medicineService.getByIdwithForm(id);
        return Result.success(medicineVO);
    }
    @PutMapping
    @ApiOperation("修改药品")
    public Result update(@RequestBody MedicineDTO medicineDTO){
        log.info("修改药品：{}",medicineDTO);
        medicineService.updateWithForm(medicineDTO);
        cleanCache("medicine_*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("药品启售与停售")
    public Result stopOrStop(@PathVariable Integer status,Long id){
        log.info("药品启售与停售：{}",status);
        medicineService.starOrStop(status,id);
        cleanCache("medicine_*");
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询药品")
    public Result<List<Medicine>> list(Long categoryId){
        List<Medicine> list = medicineService.list(categoryId);
        return Result.success(list);
    }

// 清除缓存
    private void cleanCache(String pattern){
        // 获取符合pattern的所有key
        Set keys=redisTemplate.keys(pattern);
        // 删除所有符合pattern的key
        redisTemplate.delete(keys);
    }

}
